package work1;

import java.util.ArrayList;

public class Box {
    private ArrayList<Integer> list = new ArrayList<>();
    public static final Object lock = new Object();
    private int count = 0;
    public void produce() throws InterruptedException {
        synchronized (lock) {
            while (list.size() >= 5) {
                lock.wait();
            }
            int data = count++;
            list.add(data);
            System.out.println(Thread.currentThread().getName() + " produced " + data);
            if(list.size() == 5) {
                lock.notifyAll();
            }
        }
    }
    public void consume() throws InterruptedException {
        synchronized (lock) {
            while (list.size() < 5) {
                lock.wait();
            }
            System.out.print(Thread.currentThread().getName() + " consumed ");
            for(Integer data : list) {
                System.out.print((data+" "));
            }
            System.out.println();
            list.clear();
            lock.notifyAll();
        }
    }
}
